package com.hdy.orderforthree.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import butterknife.ButterKnife;

public class BaseAdapter extends RecyclerView.Adapter<BaseAdapter.ViewHolder> {

    protected int mLayoutId;
    protected List mList;

    protected OnItemClickListener mOnItemClickListener;

    protected int mSelectedPosition = -1;

    public static class ViewHolder extends RecyclerView.ViewHolder{

        public ViewHolder(View view){
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public interface OnItemClickListener<T> {
        void OnItemClick(View view, BaseAdapter.ViewHolder holder, int position);
        void OnPlusClick(View view, BaseAdapter.ViewHolder holder, int position, T t);
        void OnMinusClick(View view, BaseAdapter.ViewHolder holder, int position, T t);
        void onDeleteClick(View view, BaseAdapter.ViewHolder holder, int position, T t);
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public BaseAdapter(int layoutId, List list){
        mLayoutId = layoutId;
        mList = list;
    }

    @Override
    public BaseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(mLayoutId, parent, false);
        RecyclerView.ViewHolder holder = new ViewHolder(view);
        return (ViewHolder) holder;
    }

    @Override
    public void onBindViewHolder(BaseAdapter.ViewHolder holder, int position) {
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void SetList(List list) {
        mList = list;
    }

    public void setSelectedPosition(int position) {
        mSelectedPosition = position;
    }

    public int getSelectedPosition() {
        return mSelectedPosition;
    }

    protected void notifySelectedChanged(final int position) {
        if (mSelectedPosition != -1) {
            notifyItemChanged(mSelectedPosition);
        }
        mSelectedPosition = position;
        notifyItemChanged(mSelectedPosition);
    }

}
